<template>
  <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" v-model="form" ref="crud" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @sort-change="sortChange">
    <!-- @on-load="onLoad" -->
    
    <template slot-scope="scope" slot="diyTime">
      {{ scope.row.startDate }} 至 {{ scope.row.endDate }}
    </template>
    <template slot-scope="scope" slot="menu">
      <el-button size="small" type="text" style="color:#f00" @click="rowDel(0, scope.row)" v-if="scope.row.isBind">解除关联</el-button>
      <el-button size="small" type="text" @click="rowDel(1, scope.row)" v-else>关联活动</el-button>
    </template>
  </avue-crud>
</template>

<script>
import { setActive,activeList } from "@/api/rb-oem/posinfo/videoDrainage";

export default {
  data() {
    return {
      exportId: '',
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        selection: false,//列表多选框
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120, //弹出表单的标题的统一宽度
        searchLabelWidth: 120, //搜索框标题宽度
        menuWidth: 100, //操作栏宽度
        emptyBtnText: '重置',
        // menu: false,
        column: [
          {
            label: "活动编号",
            prop: "ticketCode",
            type: "input"
          },
          {
            label: "活动名称",
            prop: "ticketName",
            type: "input"
          },
          {
            label: "活动种类",
            prop: "ticketKindName",
            type: "input"
          },
          {
            label: "充值类型",
            prop: "givePersonTypeName",
            type: "input"
          },
          {
            label: "活动内容",
            prop: "ticketInstructions",
            type: "input"
          },
          {
            label: "发放数量",
            prop: "totalCount",
            type: "input"
          },
          {
            label: "领券数量",
            prop: "receiveCount",
            type: "input"
          },
          {
            label: "使用数量",
            prop: "useCount",
            type: "input"
          },
          {
            label: "有效期",
            prop: "diyTime",
            type: "input",
            minWidth:200
          },
          {
            label: "状态",
            prop: "ticketStatus",
            type: "select",
            dataType: "number",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dicUrl: "/api/blade-system/dict/dictionary?code=ticket_status",
          }
        ]
      },
      data: [],
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    base64() {
      return (val) => {
        if (val) {
          if (val.indexOf('data:image/png;base64,') == -1) {
            return ('data:image/png;base64,' + val);
          } else {
            return val;
          }
        } else {
          return ''
        }
      };
    }
  },
  methods: {
    hdOpen(row){
      this.exportId = row.exportId;
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    /* 列表删除 */
    rowDel(changeType, row) {
      // changeType 1绑定 0解绑
      let param = {
        exportId: this.exportId,
        ticketCode: row.ticketCode,
        changeType
      }
      if (changeType) {
        setActive(param).then(({ data }) => {
          if (data.code === 200) {
            this.$message.success(data.msg);
            this.page.currentPage = 1;
            this.onLoad(this.page);
          } else {
            this.$message.error(data.msg);
          }
        });
      } else {
        this.$confirm("确定将选择解绑活动?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            return setActive(param);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
      }
    },
    /**
     * @func 重置
     * @desc 重置查询条件
     * */
    searchReset() {
      this.query = {};
      this.search = {};
      this.onLoad(this.page);
    },
    /**
     * @func 搜索
     * @desc 根据条件查询
     * @param {params} params --- 条件查询
     * */
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    /**
     * @func 选中单元格
     * @desc 选中单元格数据
     * @param {list} list --- 选中单元格数据-数组
     * */
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    /**
     * @func 分页页码
     * @desc 分页页码改变时会触发
     * */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      // this.refreshChange();
      this.onLoad(this.page, this.query);

    },
    /**
     * @func 当前页个数
     * @desc pageSize改变时会触发
     * */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      // this.refreshChange();
      this.onLoad(this.page, this.query);
    },
    /**
     * @func 刷新
     * @desc 点击刷新按钮触发该事件
     * */
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    /**
     * @func 初始化
     * @desc 表格初始化查询数据
     * */
    onLoad(page, params = {}) {
      this.loading = true;
      let paramObj = Object.assign(params, this.query);
      paramObj.exportId = this.exportId,

      activeList(
        page.currentPage,
        page.pageSize,
        paramObj
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/ .avue-crud__left {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>